package com.bwie.controller;

import com.alibaba.fastjson.JSON;
import com.bwie.pojo.Product;
import com.bwie.pojo.ProductType;
import com.bwie.pojo.User;
import com.bwie.service.ProductService;
import com.bwie.service.UserService;
import com.bwie.utils.R;
import com.bwie.vo.ProductVo;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import io.jsonwebtoken.Jwts;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/product")
@Api(tags = "商品微服务")
public class ProductController {


    @Autowired
    ProductService productService;

    @Autowired
    UserService userService;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    FastFileStorageClient fastFileStorageClient;

    private static final String PATHNAME = "http://192.168.189.131/";




    /**
     * @description: 商品列表查询
     * @author: xushuaihang
     * @Date: 2024/6/17 10:20
     * @param:
     * @param token
     * @return: com.bwie.utils.R
    **/
    @ApiOperation("商品列表")
    @PostMapping("/productList")
    public R productList(@RequestHeader(name = "token")String token){

        Integer userId = Jwts.parser().setSigningKey("bwie").parseClaimsJws(token).getBody().get("id", Integer.class);

        User byId = userService.getById(userId);

        ListOperations<String, String> operations = stringRedisTemplate.opsForList();

        List<Product> list = null;

        List<String> range = operations.range("product:list", 0, -1);

        if(range.isEmpty()){
            list = productService.productList();

            operations.leftPush("product:list", JSON.toJSONString(list));

            stringRedisTemplate.expire("product:list",10, TimeUnit.MINUTES);

        }else {

            list = JSON.parseArray(range.get(0),Product.class);
        }

       List<ProductType> listtype = productService.productTypeList();

       return R.ok().data("list",list).data("listType",listtype).data("userRole",byId.getUserRole());
    }

    /**
     * @description: 商品添加发布
     * @author: xushuaihang
     * @Date: 2024/6/17 10:22
     * @param:
     * @return: com.bwie.utils.R
    **/
    @ApiOperation("添加发布商品")
    @PostMapping("/addProduct")
    public R addProduct(@RequestBody ProductVo productVo){

        return productService.addProduct(productVo);

    }

    /**
     * @description:  多图片上传
     * @author: xushuaihang
     * @Date: 2024/6/17 11:17
     * @param:
     * @param file
     * @return: com.bwie.utils.R
    **/
    @ApiOperation("上传图片")
    @PostMapping("/imgUploading")
    public R imgUploading(MultipartFile file) throws IOException {

        String afterName = StringUtils.substringAfter(file.getOriginalFilename(), ".");

        String fullPath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), afterName, null).getFullPath();


        return R.ok().message("上传成功！").data("url",PATHNAME+fullPath);

    }



}
